package com.tfr.rabbitmq.controller;

import com.tfr.rabbitmq.config.RabbitMQConfig;
import com.tfr.rabbitmq.entity.Test;
import lombok.Data;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Description:
 * @ClassName: SendMsgController
 * @Author: Rain
 * @Date: 2021-10-19 15:09
 * @Version: 1.0
 **/
@RestController
public class SendMsgController {
    
    
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    /**
     * 测试
     */
    @GetMapping("/sendMsg")
    public String sendMsg(@RequestParam String msg, @RequestParam String key){
        /**
         * 发送消息
         * 参数一：交换机名称
         * 参数二：路由key: item.springboot-rabbitmq,符合路由item.#规则即可
         * 参数三：发送的消息
         */
        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,key ,msg);
        //返回消息
        return "发送消息成功！";
    }
    
    @GetMapping("/sendEntity")
    public String sendEntity( @RequestParam String key){
        /**
         * 发送消息
         * 参数一：交换机名称
         * 参数二：路由key: item.springboot-rabbitmq,符合路由item.#规则即可
         * 参数三：发送的消息
         */
        
        //对象会被序列化
        Test test = new Test(1, "zhansan", 1);
        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,key ,test,new CorrelationData(UUID.randomUUID().toString()));
        //返回消息
        return "发送消息成功！";
    }
}
